﻿using System;
using System.Text;

namespace RTF_Editor_Plugin
{
    public class RTFplugin : Notepad_X.Plugin.IPlugin {

        Notepad_X.Components.MenuItemX menuNewDefault;
        Notepad_X.Components.MenuItemX menuNewRTF;
        Notepad_X.Components.MenuItemX menuOpenIn;
        Notepad_X.Components.MenuItemX menuOpenRTF;

        Notepad_X.Main main; // Notepad X Main form  

        public string Name {
            get { return "RTF Editor Plugin"; }
        }
        public string Author {
            get { return "NohponeX"; }
        }
        public string Description {
            get { return "With this plugin you can open & edit document using the RTF text editor control"; }
        }
        public string Version {
            get { return "0.5"; }
        }

        public void Initialize() {
        //    main = Notepad_X.Static.MainInstance;

            Notepad_X.Classes.DocumentTypes.AddType(typeof(RTFDocument));
            return;
            menuNewDefault = new Notepad_X.Components.MenuItemX();
            menuNewDefault.Text = "DefaultICSharp editor";
            menuNewDefault.Name = "ICSharp";
            menuNewDefault.Icon = Notepad_X.Classes.Icons.@new;
            menuNewDefault.String = Notepad_X.Classes.Strings.none;
            menuNewDefault.Click += new EventHandler(main.handler_new);


            menuNewRTF = new Notepad_X.Components.MenuItemX();
            menuNewRTF.Name = "RTF";
            menuNewRTF.Text = "RTF editor";
            menuNewRTF.Icon = Notepad_X.Classes.Icons.@new;
            menuNewRTF.String = Notepad_X.Classes.Strings.none;
            menuNewRTF.Click += new EventHandler(menuItem_Click);

            main.system_menuItem_Get("Menu_File_New").Click -= new EventHandler(main.handler_new);

            main.system_menuItem_Get("Menu_File_New").DropDownItems.Add(menuNewDefault);
            main.system_menuItem_Get("Menu_File_New").DropDownItems.Add(menuNewRTF);

            if (main.system_menuItem_Get("Menu_File").DropDownItems.ContainsKey("Menu_File_OpenIn")) {
                menuOpenIn = (Notepad_X.Components.MenuItemX)main.system_menuItem_Get("Menu_File_OpenIn");
            } else {
                menuOpenIn = new Notepad_X.Components.MenuItemX();
                menuOpenIn.Icon = Notepad_X.Classes.Icons.open;
                menuOpenIn.String = Notepad_X.Classes.Strings.none;
                menuOpenIn.Text = "Open in...";
                menuOpenIn.Name = "Menu_File_OpenIn";
            }

            menuOpenRTF = new Notepad_X.Components.MenuItemX();
            menuOpenRTF.Name = "Menu_File_OpenIn_RTF";
            menuOpenRTF.Text = "Open in RTF Editor";
            menuOpenRTF.Icon = Notepad_X.Classes.Icons.open;
            menuOpenRTF.String = Notepad_X.Classes.Strings.none;

            menuOpenIn.DropDownItems.Add(menuOpenRTF);

            menuOpenRTF.Click += new EventHandler(menuOpenRTF_Click);
            main.system_menuItem_Get("Menu_File").DropDownItems.Insert((1 + main.system_menuItem_Get("Menu_File").DropDownItems.IndexOfKey("Menu_File_Open")),menuOpenIn);

            Notepad_X.Components.Static.UpdateMenuItemX(menuNewDefault, true);
            Notepad_X.Components.Static.UpdateMenuItemX(menuNewRTF, true);
            Notepad_X.Components.Static.UpdateMenuItemX(menuOpenIn, true);
            Notepad_X.Components.Static.UpdateMenuItemX(menuOpenRTF, true);

        }


        public void Dispose() {
            //main.system_menuItem_Get("Menu_File_New").Click += new EventHandler(main.handler_new);

            //main.system_menuItem_Get("Menu_File_New").DropDownItems.RemoveByKey("RTF");
            //main.system_menuItem_Get("Menu_File_New").DropDownItems.RemoveByKey("ICSharp");
            //main.system_menuItem_Get("Menu_File").DropDownItems.RemoveByKey("Menu_File_OpenIn");
            //main.Menu_.Items.Remove(menuNewRTF);
        }
        void menuOpenRTF_Click(object sender, EventArgs e) {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();

            dialog.Title = Notepad_X.Main.app_language[Notepad_X.Classes.Strings.Open];
            dialog.Multiselect = true;
            try {
                if (!Notepad_X.Main.app_settings.extension_open.Contains("*.*"))
                    Notepad_X.Main.app_settings.extension_open = "All files (*.*)|*.*|" + Notepad_X.Main.app_settings.extension_open;

                dialog.Filter = Notepad_X.Main.app_settings.extension_open;
            } catch { }

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                foreach (var file in dialog.FileNames) {
                    int id= newDocument();
                    main.Documents[id].open(file);

                    main.Panel.SelectedTab.Text = System.IO.Path.GetFileName(file);

                    main.system_filewatcher(id);

                    //Document Event 
                    //new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.Open);
                   
                    main.handler_PanelTabChanged(null, null);
                }
            }
        }

        int newDocument() {
            int id = main.Documents.Count;
            main.Documents.Add(new RTFDocument(id));
            main.Documents[id].lineNumbers(Notepad_X.Main.app_settings.line_numbers);

            main.Documents[id].font = Notepad_X.Main.app_settings.font;

            main.Documents[id].tab_page.ImageKey = "documentok";
            main.Documents[id].tab_page.Text = Notepad_X.Main.app_language[Notepad_X.Classes.Strings.Document] + " " + (id + 1).ToString();

            main.Panel.Controls.Add(main.Documents[id].tab_page);
            main.Panel.SelectTab(main.Documents[id].tab_page);
            main.handler_PanelTabChanged(null, null);

            return id;
        }
        void menuItem_Click(object sender, EventArgs e) {
            int id = newDocument();
            //new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.New);
            //main.DocumentEvent(new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.New));    
        }
     

    }
}
